using System;
using System.Web.UI;
using mojoPortal.Business;
using mojoPortal.Web.Framework;
using mojoPortal.Web.UI;
using mojoPortal.Web;
using ud_Article.Business;
using Resources;
using System.Collections;

namespace ud_Article.UI
{
    public partial class ManagePost : mojoBasePage
    {
        protected ArticleConfiguration config = new ArticleConfiguration();
        private int pageId = -1;
        private int moduleId = -1;
        private bool userCanEdit;
        private int countOfDrafts;

        #region OnInit

        override protected void OnInit(EventArgs e)
        {
            LoadParams();
            LoadPanels();
            Load += Page_Load;
            base.OnInit(e);
            LoadSettings();
        }

        #endregion

        private void LoadPanels()
        {
            LoadSideContent(config.ShowLeftPanelSetting, config.ShowRightPanelSetting, config.ShowTopPanelSetting, config.ShowBottomPanelSetting, config.ShowNumberModuleSetting);
        }

        private void LoadSettings()
        {
            userCanEdit = UserCanEditModule(moduleId);
            if (userCanEdit)
            {
                countOfDrafts = Article.CountOfDrafts(moduleId);
            }
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (!Request.IsAuthenticated)
            {
                SiteUtils.RedirectToLoginPage(this);
                return;
            }
            SecurityHelper.DisableBrowserCache();


            if (!userCanEdit)
            {
                SiteUtils.RedirectToEditAccessDeniedPage();
            }

            PopulateLabels();

            if (!Page.IsPostBack)
            {
                PopulateControls();
            }

        }


        private void PopulateLabels()
        {
            Title = SiteUtils.FormatPageTitle(siteSettings, ArticleResources.AdminManagePostLabel);
            if (Master != null)
            {
                Control c = Master.FindControl("Breadcrumbs");
                if (c != null)
                {
                    BreadcrumbsControl crumbs = (BreadcrumbsControl)c;
                    crumbs.ForceShowBreadcrumbs = true;

                }
            }

            moduleTitle.ModuleInstance = GetModule(moduleId);
            if ((userCanEdit) && (countOfDrafts > 0))
            {
                //BlogEditCategoriesLabel
                moduleTitle.LiteralExtraMarkup =
                    "&nbsp;<a href='"
                    + SiteRoot
                    + "/Article/EditPost.aspx?pageid=" + pageId.ToInvariantString()
                    + "&amp;mid=" + moduleId.ToInvariantString()
                    + "' class='ModuleEditLink' title='" + ArticleResources.BlogAddPostLabel + "'><img src='/Data/Icon16x16/Modify.png' alt='" + ArticleResources.BlogAddPostLabel + " |'</img></a>"
                    + "&nbsp;<a href='"
                    + SiteRoot
                    + "/Article/EditCategory.aspx?pageid=" + pageId.ToInvariantString()
                    + "&amp;mid=" + moduleId.ToInvariantString()
                    + "' class='editcategorieslink' title='" + ArticleResources.BlogEditCategoriesLabel + "'><img src='/Data/Icon16x16/Folder.png' alt='" + ArticleResources.BlogEditCategoriesLabel + " |'</img></a>"
                    + "&nbsp;<a href='"
                    + SiteRoot
                    + "/Article/Drafts.aspx?pageid=" + pageId.ToInvariantString()
                    + "&amp;mid=" + moduleId.ToInvariantString()
                    + "' class='draftlink' title='" + ArticleResources.BlogDraftsLink + "'><img src='/Data/Icon16x16/Bubble.png' alt='" + ArticleResources.BlogDraftsLink + " |'</img></a>"
                    + "&nbsp;<a href='"
                    + SiteRoot
                    + "/Article/ManagePost.aspx?pageid=" + pageId.ToInvariantString()
                    + "&amp;mid=" + moduleId.ToInvariantString()
                    + "' class='managepostlink' title='" + ArticleResources.AdminManagePostLabel + "'><img src='/Data/Icon16x16/Text.png' alt='" + ArticleResources.AdminManagePostLabel + "'</img></a>";
            }
            else if (userCanEdit)
            {
                moduleTitle.LiteralExtraMarkup =
                    "&nbsp;<a href='"
                    + SiteRoot
                    + "/Article/EditPost.aspx?pageid=" + pageId.ToInvariantString()
                    + "&amp;mid=" + moduleId.ToInvariantString()
                    + "' class='ModuleEditLink' title='" + ArticleResources.BlogAddPostLabel + "'><img src='/Data/Icon16x16/Modify.png' alt='" + ArticleResources.BlogAddPostLabel + " |'</img></a>"
                    + "&nbsp;<a href='"
                    + SiteRoot
                    + "/Article/EditCategory.aspx?pageid=" + pageId.ToInvariantString()
                    + "&amp;mid=" + moduleId.ToInvariantString()
                    + "' class='editcategorieslink' title='" + ArticleResources.BlogEditCategoriesLabel + "'><img src='/Data/Icon16x16/Folder.png' alt='" + ArticleResources.BlogEditCategoriesLabel + " |'</img></a>"
                    + "&nbsp;<a href='"
                    + SiteRoot
                    + "/Article/ManagePost.aspx?pageid=" + pageId.ToInvariantString()
                    + "&amp;mid=" + moduleId.ToInvariantString()
                    + "' class='managepostlink' title='" + ArticleResources.AdminManagePostLabel + "'><img src='/Data/Icon16x16/Text.png' alt='" + ArticleResources.AdminManagePostLabel + "'</img></a>";
            }
        }


        private void PopulateControls()
        {
            postList.ModuleId = moduleId;
            postList.PageId = pageId;
            postList.SiteRoot = SiteRoot;
            postList.ImageSiteRoot = ImageSiteRoot;
            postList.IsEditable = userCanEdit;
            postList.Config = config;
        }

        private void LoadParams()
        {
            pageId = WebUtils.ParseInt32FromQueryString("pageid", -1);
            moduleId = WebUtils.ParseInt32FromQueryString("mid", -1);
            Hashtable moduleSettings = ModuleSettings.GetModuleSettings(moduleId);
            config = new ArticleConfiguration(moduleSettings);
        }
    }
}
